package com.test.slice;

import com.ms.banner.Banner;
import com.ms.banner.BannerConfig;
import com.ms.banner.util.Utils;
import com.test.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import static com.test.MyApplication.getDataInt;
import static com.test.MyApplication.getDataStr;

public class SimpleFragmentAbilitySlice extends AbilitySlice {
    private StackLayout rootContainer;
    private DirectionalLayout home,more;
    private Text tHome,tMore;
    private Image iHome,iMore;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bottom_fragment);

        rootContainer = (StackLayout) findComponentById(ResourceTable.Id_root_center);
        home = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_home);
        more = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_more);
        tHome = (Text) findComponentById(ResourceTable.Id_t_home);
        tMore = (Text) findComponentById(ResourceTable.Id_t_more);
        iHome = (Image) findComponentById(ResourceTable.Id_i_home);
        iMore = (Image) findComponentById(ResourceTable.Id_i_more);

        Component component1 = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_fragment_home,rootContainer,false);
        Text text = (Text) component1.findComponentById(ResourceTable.Id_text_select_style);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new BannerLocalAbilitySlice(),new Intent());
            }
        });
        Banner banner1 = (Banner) component1.findComponentById(ResourceTable.Id_banner1);
        banner1.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, (int) (Utils.getInstance().getScreenPiex(this).height/4)));
        banner1.setBannerTitles(getDataStr())
                .setPages(getDataInt(), new CustomViewHolder())
                .setAutoPlay(false)
                .setDelayTime(3000)
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();
        Component component2 = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_fragment_more,rootContainer,false);
        Banner banner2 = (Banner) component2.findComponentById(ResourceTable.Id_banner2);
        banner2.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, (int) (Utils.getInstance().getScreenPiex(this).height/4)));
        banner2.setBannerTitles(getDataStr())
                .setPages(getDataInt(), new CustomViewHolder())
                .setAutoPlay(true)
                .setDelayTime(3000)
                .setIndicatorGravity(BannerConfig.CENTER)
                .start();
        rootContainer.addComponent(component1);

        home.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                tHome.setTextColor(Color.GREEN);
                iHome.setImageAndDecodeBounds(ResourceTable.Media_green_star);
                tMore.setTextColor(Color.RED);
                iMore.setImageAndDecodeBounds(ResourceTable.Media_red_star);
                rootContainer.removeAllComponents();
                rootContainer.addComponent(component1);
            }
        });
        more.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                tHome.setTextColor(Color.RED);
                iHome.setImageAndDecodeBounds(ResourceTable.Media_red_star);
                tMore.setTextColor(Color.GREEN);
                iMore.setImageAndDecodeBounds(ResourceTable.Media_green_star);
                rootContainer.removeAllComponents();
                rootContainer.addComponent(component2);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
